var t = getApp()
Page({
  data: {
    site:'',
    store:'',
    userinfo:wx.getStorageSync('userinfo'),
    navHeight:t.globalData.navHeight,
    info:'',
    setInter:'',
    getInfo:'',
    buttonStaus:false,
  },
  onLoad: function (options) {
    console.log(options);
    var that = this
    if (options) {
      this.setData({
        site:options.site,
        store:options.store
      })
    }else{
      wx.switchTab({
        url: '/pages/index/index',
      })
    }
    wx.login({
      success: res => {
        t.request('/login',{code:res.code},function(r){
          if(r.result.userInfo.user_id > 0){
            wx.setStorageSync('userinfo',r.result.userInfo);
            that.setData({
              userinfo:r.result.userInfo
            })
            that.get_info_fun();
            that.data.getInfo = setInterval(() => {
              setTimeout(that.get_info_fun, 0);
            }, 6000)
          }
        })
      }
    })
  },
  onShow(e){
    var that = this
  },
  onReady(){
    console.log('onReady')
    setTimeout(this.setButton,30000);
  },
  //30秒隐藏按钮
  setButton(){
    console.log('buttonStaus')
    clearInterval(this.data.getInfo)
    this.setData({
      buttonStaus:true
    })
  },
  get_info_fun(e){
    var that = this
    if (!wx.getStorageSync('userinfo')) {
      this.login = this.selectComponent("#login")
      t.show_login()
      return
    }else{
      var o = {
        user_id:wx.getStorageSync('userinfo').user_id,
        store:that.data.store,
        site:that.data.site
      }
      t.request('/settle',o,function(e){
        console.log(e);
        if (e.code==200) {
          that.setData({
            info:e.result
          })
        }else if(e.code==400){ //错误情况下 返回首页-wlqiao-2024年8月1日21:51:04
          clearInterval(that.data.getInfo);
          wx.showModal({
            title: e.msg,
            showCancel:false,
            success(res){
              wx.switchTab({
                url: '/pages/index/index',
              })
            }
          })
        }
      })
    }
  },
  onPullDownRefresh: function () {
    wx.showToast({
      title: '正在刷新数据...',
      icon: 'loading',
      duration: 2000
    });
    this.onLoad()
    wx.stopPullDownRefresh();//停止刷新操作
  },
  leave(e){
    console.log(e)
    
    var that = this
    if (parseFloat(this.data.info.cost)>parseFloat(this.data.info.balance)) {
      wx.showModal({
        showCancel:false,
        title:'提示',
        content:'余额不足请充值'
      })
      return
    }else{
      t.request('/settle/leave',{site:that.data.site},function(e){
        if (e.code==200) {
          wx.showToast({
            title: '结算成功',
          })
          clearInterval(that.data.getInfo)
          that.data.setInter = setInterval(() => {
            setTimeout(that.get_status, 0);
          }, 1000)
        }
      })
    }
  },
  getPhoneNumber(e){
    const that = this,
      iv = e.detail.iv,
      da = e.detail.encryptedData,
      carnum = ''
    console.log(e);
    if (!e.detail.encryptedData) return wx.showToast({ title: '授权失败,请重新授权！',icon:'none'})
    wx.checkSession({
      success: (res) => {
        wx.login({
          success(e){
            console.log(e);
            wx.showLoading({
              title: '请稍后',
            })
            t.request('/users/get_session',{code: e.code,iv:iv,da:da},function(event){
              wx.hideLoading({})
              if (event.code!==200) return wx.showToast({ title: event.msg,icon:'none' })
              if (event.code==200) {
                console.log(event.result.phoneNumber)
                wx.setStorageSync('tel', event.result.phoneNumber)
                that.setData({
                  tel:event.result.phoneNumber,
                })
                that.leave()
              }
            })
          }
        })
      },
    })
  },
  // 充值按钮
  invest(e){
    var that = this
    wx.navigateTo({
      url: '/pages/personCenter/chooseStore/invest/invest?store_id=' + that.data.info.store_id + '&card_id='+that.data.info.card_id,
    })
  },
  get_status(e){
    var that = this
    t.request('/settle/open_status',{site:this.data.site},function(e){
      if (e.code==200) {
        wx.showToast({
          title: '开门成功',
          duration:2000,
        })
        clearInterval(that.data.setInter)
        wx.switchTab({
          url: '/pages/index/index',
        })
      }else{
        wx.showLoading({
          title: '正在开门请稍后……',
          duration:3000,
        })
      }
    })
  },
  pay_once(e){
    const that = this
    wx.showLoading({ title: '加载中' })
    t.request('/settle/pay_once',{site:that.data.site},function(e){
      if (e.code!==200) return wx.showToast({ title:e.msg,icon:'none' })
      console.log(e);
      var params = e.result.params
      wx.requestPayment({
        nonceStr: params.nonceStr,
        package: params.package,
        paySign: params.paySign,
        signType: 'MD5',
        timeStamp: params.timeStamp,
        'success'(e){
          wx.hideLoading({ success: (res) => {} })
          clearInterval(that.data.getInfo)
          wx.showToast({
            title: '支付成功',
          })
          wx.switchTab({
            url: '/pages/index/index',
          })
        },
        'fail'(e){
          wx.hideLoading({
            success: (res) => {},
          })
          wx.showModal({
            title: '您取消了支付！',
            showCancel:false,
            complete:function(){
            }
          })
        },
      })
    })
  }
})